<template>
  <div class="carManage">
    <c-title :hide="false" :text="'车辆管理'"></c-title>
    <div class="list">
      <van-cell title="车辆申诉" is-link title-style="text-align: left;" @click="tocarAppeal" />
    </div>
    <div class="carList" v-for="item in carList" :key="item.id">
      <div class="carList_l">
        <div class="car">{{ item.license_no }}</div>
        <div class="tagBox" v-if="item.group_name">{{ item.group_name }}</div>
      </div>
      <div class="carList_r" @click="deleteBtn(item)">解绑</div>
    </div>
    <div class="bottom_btn set-pc-style">
      <div class="btn" @click="show = true">绑定车牌号</div>
    </div>

    <van-popup v-model="show" position="bottom" round safe-area-inset-bottom="true">
      <van-nav-bar title="绑定车牌号" @click-left="onClickLeft" @click-right="onClickRight">
        <template #right>
          <span>确定</span>
        </template>
        <template #left>
          <span style="color:#999999">取消</span>
        </template>
      </van-nav-bar>
      <div class="contenShow">
        <div class="car_lis">
          <template v-for="(item, index) in selectProvince">
            <div class="crile" :key="index" v-if="index == 2">·</div>
            <div class="proivesBtn" :class="selectInd == index ? 'sd' : ''" @click="provincesShowTap(index)" :key="index">
              <span>{{ item }}</span>
              <div class="cursor"></div>
            </div>
          </template>
        </div>
        <div class="radio_line">
          <van-radio-group v-model="radio" direction="horizontal" @change="changeRadioT">
            <van-radio name="0" checked-color="#FE5E56">汽车</van-radio>
            <van-radio name="1" checked-color="#FE5E56">摩托车</van-radio>
          </van-radio-group>
        </div>
        <div class="radio_line">
          <van-radio-group v-model="licensePlate" direction="horizontal">
            <van-radio name="0" checked-color="#FE5E56" v-show="radio != '1'">绿牌</van-radio>
            <van-radio name="1" checked-color="#FE5E56">蓝牌</van-radio>
            <van-radio name="2" checked-color="#FE5E56">黄牌</van-radio>
          </van-radio-group>
        </div>
        <car_number_keyboard @clickChild="clickChild" @delete="child_Delete" :ind="selectInd" :show="provincesShow" :keyboardType="keyboardType"></car_number_keyboard>
      </div>
    </van-popup>
  </div>
</template>
<script>
import car_number_keyboard from "./components/car-number-keyboard.vue";
import { scrollMixin } from "utils/mixin";
export default {
  mixins: [scrollMixin],
  data() {
    return {
      show: false,
      provincesShow: false,
      selectProvince: ["", "", "", "", "", "", "", ""],
      radio: "0",
      licensePlate: "0",
      keyboardType: 1,
      selectInd: null,
      carList: [],

      isLoadMore: true,
      page: 1,
      total_page: 0
    };
  },
  components: { car_number_keyboard },
  activated() {
    this.init();
    this.getData();
  },
  watch: {
    listChange: function(nval, oval) {
      if (nval) {
        this.selectProvince = ["", "", "", "", "", "", "", ""];
        this.selectInd = 0;
        this.keyboardType = 1;
      } else {
        this.selectProvince = ["", "", "", "", "", "", ""];
        this.selectInd = 0;
        this.keyboardType = 1;
      }
    }
  },
  computed: {
    listChange() {
      const { radio, licensePlate } = this;
      if (radio == "0" && licensePlate == "0") {
        return true;
      } else {
        return false;
      }
    }
  },
  methods: {
    async deleteBtn(item) {
      this.$dialog
        .confirm({
          message: "确定要解绑此车辆吗？"
        })
        .then(() => {
          this.deleteCar(item);
        });
    },
    async deleteCar(item) {
      let { data, msg, result } = await $http.post("plugin.car-manage.frontend.controllers.car.untie-car", { car_id: item.id }, "");
      if (result) {
        this.init();
        this.getData();
      } else {
        this.$toast(msg);
      }
    },
    changeRadioT() {
      if (this.radio == "1" && this.licensePlate == "0") this.licensePlate = "1";
    },
    tocarAppeal() {
      this.$router.push(this.fun.getUrl("carAppeal_list", { source: 1 }));
    },
    onClickLeft() {
      this.init();
    },
    init() {
      this.selectProvince = ["", "", "", "", "", "", "", ""];
      this.radio = "0";
      this.licensePlate = "0";
      this.provincesShow = false;
      this.keyboardType = 1;
      this.selectInd = null;
      this.show = false;
      this.page = 1;
      this.isLoadMore = true;
      this.total_page = 0;
      this.carList = [];
    },
    async getData() {
      let { data, msg, result } = await $http.post("plugin.car-manage.frontend.controllers.car.index", {}, "");
      if (result) {
        this.carList = data.data;
        this.total_page = data.last_page;
        if (!this.total_page) {
          this.total_page = 0;
        }
        this.isLoadMore = true;
      } else {
        this.$toast(msg);
      }
    },
    async getMoreData() {
      if (!this.isLoadMore) return;
      this.isLoadMore = false;
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page += 1;
        let { data, msg, result } = await $http.post("plugin.car-manage.frontend.controllers.car.index", { page: this.page }, "");
        this.isLoadMore = true;
        if (result) {
          this.carList = this.carList.concat(data.data);
        } else {
          this.$toast(msg);
          this.isLoadMore = false;
          this.page = this.page - 1;
        }
      }
    },
    async onClickRight() {
      let car_info = {
        license_no: [...this.selectProvince].join(""),
        license_colour: this.licensePlate,
        car_type: this.radio
      };
      if (this.fun.isTextEmpty(car_info.license_no)) {
        this.$toast("请输入车牌号");
        return;
      }
      if (this.licensePlate == "0" && this.radio == "0") {
        // 如果是绿牌并且是汽车的话，车牌要8为字符串
        if (car_info.license_no.length < 8) {
          this.$toast("请输入正确车牌号");
          return;
        }
      } else {
        if (car_info.license_no.length < 7) {
          this.$toast("请输入正确车牌号");
          return;
        }
      }
      let { data, msg, result } = await $http.post("plugin.car-manage.frontend.controllers.car.bind-car", { car_info: car_info }, "");
      if (result) {
        this.$toast(msg);
        this.init();
        this.getData();
      } else {
        this.$toast(msg);
      }
      // 请求绑定接口 后再走init初始化方法
    },
    provincesShowTap(index) {
      this.provincesShow = true;
      this.selectInd = index;
      if (index == 0) {
        this.keyboardType = 1;
      } else if (index >= 1) {
        this.keyboardType = 2;
      }
    },
    clickChild(e) {
      this.$set(this.selectProvince, e.ind, e.key);
      if (e.ind < this.selectProvince.length - 1) {
        this.selectInd = e.ind + 1;
        if (this.selectInd == 0) {
          this.keyboardType = 1;
        } else if (this.selectInd > 0) {
          this.keyboardType = 2;
        }
      }
    },
    child_Delete(e) {
      this.$set(this.selectProvince, e.ind, "");
    }
  }
};
</script>
<style lang="scss" scoped>
.contenShow {
  padding: 0 1.56rem;
  margin-top: 2rem;
  .radio_line {
    margin-top: 0.56rem;
  }
  .radio_line:last-child {
    margin-bottom: 2rem;
  }
  .car_lis {
    display: flex;
    align-items: center;
    justify-content: space-around;
  }
  input,
  .proivesBtn {
    border: 0.03rem solid #d6d6dc;
    width: 2.19rem;
    height: 2.19rem;
    background: #ffffff;
    border-radius: 0.5rem;
    margin-right: 0.25rem;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
  }
  .sd {
    border: 1px solid #0099ff;
    box-shadow: 0px 0px 10px #0099ff;
    position: relative;
    .cursor {
      width: 0.0625rem;
      height: 0.9375rem;
      background-color: #0099ff;
      animation: flashing 1.2s infinite steps(1, start);
    }
  }
  @keyframes flashing {
    0%,
    100% {
      background-color: #0099ff;
    }

    50% {
      background-color: transparent;
    }
  }

  .proivesBtn {
    flex-shrink: 0;
  }
  .proivesBtn:nth-child(2) {
    margin-right: 0;
  }

  .crile {
    font-size: 1rem;
    border: 1rem;
    font-weight: bold;
    margin: 0 0.63rem;
  }
}
.carManage {
  padding: 0.63rem 0.75rem;
  padding-bottom: 5rem;
  box-sizing: border-box;
  .list {
    .van-cell {
      border-radius: 0.5rem;
    }
  }
  .bottom_btn {
    width: 100%;
    position: fixed;
    left: 0;
    bottom: 0;
    background: #ffffff;
    padding: 0.63rem 0.75rem;
    .btn {
      height: 2.5rem;
      background: #f15353;
      border-radius: 1.28rem;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 0.94rem;
      color: #ffffff;
    }
  }
  .carList {
    margin-top: 0.63rem;
    display: flex;
    padding: 1rem 0.75rem;
    background: #ffffff;
    border-radius: 0.5rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    overflow: hidden;
    .carList_l {
      display: flex;
      overflow: hidden;
      align-items: center;
      flex: 1;
      .car {
        font-weight: bold;
        font-size: 1.31rem;
        flex-shrink: 0;
      }
      .tagBox {
        // flex-shrink: 0;
        // overflow: hidden;
        // height: 1.13rem;
        // background: #feeded;
        // margin-left: 0.25rem;
        // padding: 0.19rem 0.28rem;
        // border-radius: 0.25rem;
        // font-size: 0.75rem;
        // color: #f15353;
        // display: flex;
        // align-items: center;
        // box-sizing: border-box;
        background: #feeded;
        padding: 0.19rem 0.28rem;
        border-radius: 0.25rem;
        font-size: 0.75rem;
        color: #f15353;
        text-align: left;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
        text-overflow: ellipsis;
        margin-left: 0.5rem;
      }
    }
    .carList_r {
      flex-shrink: 0;
      background-color: #f15353;
      color: #fff;
      height: 1.5rem;
      line-height: 1.5rem;
      width: 2.5rem;
      border-radius: 0.5rem;
      margin-left: 0.5rem;
    }
  }
}
</style>
